package com.gui.mianshiya.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gui.mianshiya.mapper.WishCollectionMapper;
import com.gui.mianshiya.model.dto.collection.CollectionAddRequest;
import com.gui.mianshiya.model.entity.Question;
import com.gui.mianshiya.model.entity.WishCollection;
import com.gui.mianshiya.model.dto.collection.WishCollectionQuestion;
import com.gui.mianshiya.model.vo.QuestionVO;
import com.gui.mianshiya.service.QuestionService;
import com.gui.mianshiya.service.WishCollectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Title: CollectionServiceImpl
 * @Author quit
 * @Package com.gui.mianshiya.service.impl
 * @Date 2025/4/21 13:57
 * @description: 收藏
 */

@Service
@Slf4j
public class CollectionServiceImpl extends ServiceImpl<WishCollectionMapper, WishCollection>  implements WishCollectionService {

    @Resource
    private WishCollectionMapper wishCollectionMapper;

    @Resource
    private QuestionService questionService;


    @Override
    public int  addCollectionUserId(CollectionAddRequest collectionAddRequest) {
        LambdaQueryWrapper<WishCollection> lambdaQuery = Wrappers.lambdaQuery(WishCollection.class);
        lambdaQuery
                .eq(WishCollection::getUserId, collectionAddRequest.getUserId())
                .eq(WishCollection::getQuestionId,collectionAddRequest.getQuestionId());
        WishCollection wishCollection = this.getOne(lambdaQuery);
        boolean flag  = false;
        //如果是空
        if(wishCollection==null){
            wishCollection = new WishCollection();
            wishCollection.setUserId(collectionAddRequest.getUserId());
            wishCollection.setQuestionId(collectionAddRequest.getQuestionId());
            wishCollection.setCreateTime(new Date());
            wishCollectionMapper.insert(wishCollection);
            return 1;
        }else if (wishCollection.getIsFlag() == 1){ //不是空 看isDelete是否为1 // 为1则收藏题目 为0则取消收藏
            wishCollection.setIsFlag(0);
            flag = true;
        }else {
            wishCollection.setIsFlag(1);
            flag = false;
        }
        wishCollectionMapper.updateById(wishCollection);
        if (flag ==  true) {
            return 1;
        }else {
            return 0;
        }
    }
    @Override
    public int getCollectionUserId(CollectionAddRequest collectionAddRequest) {
        LambdaQueryWrapper<WishCollection> lambdaQuery = Wrappers.lambdaQuery(WishCollection.class);
        lambdaQuery
                .eq(WishCollection::getUserId, collectionAddRequest.getUserId())
                .eq(WishCollection::getQuestionId,collectionAddRequest.getQuestionId())
                .eq(WishCollection::getIsFlag,0);
        WishCollection one = this.getOne(lambdaQuery);
        if (one == null) {
            return 0;
        }else {
            return 1;
        }
    }

    @Override
    public Page<QuestionVO> getCollectionVoPage(WishCollectionQuestion wishCollectionQuestion, HttpServletRequest request) {
        // 1. 获取用户收藏的题目ID列表
        LambdaQueryWrapper<WishCollection> collectionQuery = new LambdaQueryWrapper<>();
        collectionQuery
                .select(WishCollection::getQuestionId) // 只查询题目ID字段
                .eq(WishCollection::getUserId, wishCollectionQuestion.getUserId())
                .eq(WishCollection::getIsFlag, 0);
        List<WishCollection> collections = this.list(collectionQuery);
        if (collections.isEmpty()) {
            return new Page<>(wishCollectionQuestion.getCurrent(), wishCollectionQuestion.getPageSize(), 0);
        }
        // 2. 提取题目ID集合
        List<Long> questionIds = collections.stream()
                .map(WishCollection::getQuestionId)
                .collect(Collectors.toList());

        // 3. 构建题目分页查询
        List<Question> questions = questionService.listByIds(questionIds);

        Page<Question> page = new Page<>(wishCollectionQuestion.getCurrent(), wishCollectionQuestion.getPageSize());
        LambdaQueryWrapper<Question> questionQuery = new LambdaQueryWrapper<>();
        questionQuery
                .in(Question::getId, questionIds)
                .orderByDesc(Question::getCreateTime); // 按收藏时间排序
        Page<Question> questionPage = questionService.page(page, questionQuery);
        List<QuestionVO> questionVO = new ArrayList<QuestionVO>();
        // 4. 执行分页查询
        questionPage.convert(question -> {
            questionVO.add(QuestionVO.objToVo(question));
            return null;
        });
        return new Page<QuestionVO>().setRecords(questionVO).setTotal(questionPage.getTotal());
    }
}
